<template>
    <div style="padding: 30px 20px;">
        <el-form ref="form" :model="form" label-width="160px" size="mini" :inline="true" style="margin-bottom: 10px;">
            <el-row :gutter="24">
                <el-col :span="6">
                    <el-form-item label="资源资产编码" style="width: 100%">
                        <el-input v-model="form.sourceAssetCode" class="pms-zr-input" />
                    </el-form-item>
                </el-col>
                <el-col :span="6">
                    <el-form-item label="标签ID" style="width: 100%">
                        <el-input v-model="form.labelId" class="pms-zr-input" />
                    </el-form-item>
                </el-col>
                <el-col :span="6">
                    <el-form-item label="是否已绑定" style="width: 100%">
                        <el-select v-model="form.isSelect" placeholder="请选择" clearable>
                            <el-option label="是" value="1" />
                            <el-option label="否" value="0" />
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="6" style="text-align: right;padding-top: 5px">
                    <el-button size="mini" class="pms-zr-button-primary" @click="getTable">查询</el-button>
                </el-col>
            </el-row>
        </el-form>
        <div style="font-weight: bold;padding-bottom: 10px;color: #333333">有标签ID物资</div>
        <el-table
                ref="multipleTable"
                :data="tableData"
                max-height="400px"
                border
                style="width: 100%;"
                row-key="sourceAssetCode"
                @selection-change="handleSelectionChange"
        >
            <el-table-column :selectable="({isSelect,subPrjId})=> isSelect !== '1' || isSelect === '1' && subPrjId === prjId" :reserve-selection="true" type="selection" align="center" width="55" />
            <el-table-column align="center" prop="sourceAssetCode" label="资源资产编码" />
            <el-table-column align="center" prop="labelId" label="标签ID" />
            <el-table-column align="center" prop="subType" label="建设子专业">
                <template slot-scope="scope">
                    <el-select v-model="scope.row.subType" placeholder="请选择" style="width: 90%" class="pms-select" size="mini" :disabled="scope.row.isSelect === '1' && scope.row.subPrjId !== prjId || scope.row.subSpecList.length === 1">
                        <el-option
                                v-for="item in scope.row.subSpecList"
                                :key="item.subSpecCode"
                                :label="item.subSpecName"
                                :value="item.subSpecCode"
                        />
                    </el-select>
                </template>
            </el-table-column>
            <el-table-column align="center" prop="subPrjCode" label="已绑定的单项工程编码" />
        </el-table>
        <div v-if="showNoLabel">
            <div style="font-weight: bold;padding: 20px 0 10px;color: #333333">无标签ID物资</div>
            <div style="display: flex">
                <div style="width: 80px">绑定数量：</div>
                <el-input
                    style="width: 150px"
                    v-model="bindNum"
                    type="number"
                    class="pms-custom"
                />
            </div>
        </div>
        <div class="dialog-footer" style="display: flex;justify-content: center;margin-top: 15px">
            <el-button class="pms-button-primary" size="small" @click="save">确 定</el-button>
        </div>
    </div>
</template>

<script>
    import {getQueryObject, msgInfoEl} from "@/utils";
    import {
        bindAssetLabelList,
        checkMateLabelPrecision,
        getAssetLabelList
    } from "@/api/acceptanceManagement/materialServiceBind";

export default {
    name: 'ResourceCodeLabelOne',
    components: {
    },
    props: {},
    data() {
        return {
            tableData:[],
            form: {
                sourceAssetCode: '',
                labelId: '',
                isSelect: '',
            },
            multipleSelection:[],
            prjId:'',
            osDetailId:'',
            status:false,
            bindNum:'',
            showNoLabel:false,
            osAmount:'',
            categoryCode:'',
            outputType:''
        }
    },
    computed: {},
    created() {
        const { prjId,osDetailId,categoryCode,ifSelectAssetCode,osAmount,outputType } = getQueryObject()
        this.prjId=prjId;
        this.osDetailId=osDetailId
        this.categoryCode=categoryCode
        this.showNoLabel= ifSelectAssetCode === '1'
        this.osAmount=osAmount
        this.outputType=outputType
    },
    mounted() {
        this.getTableMultipleSelection();
    },
    methods: {
        save() {
            if(this.showNoLabel){
                this.saveNoLabel();
            }else{
                this.saveLabel();
            }
        },
        checkSaveNoLabelOne(){
            if(this.multipleSelection.length === 0 && (!this.bindNum || +this.bindNum === 0)){
                msgInfoEl({
                    type:'error',
                    message:'请选择对应的签码数据或填写无标签ID物资绑定数量',
                })
                return Promise.reject('error')
            }
        },
        checkSaveNoLabelTwo(){
            if(+this.bindNum < 0){
                msgInfoEl({
                    type:'error',
                    message:'无标签ID物资绑定数量应大于等于0',
                })
                return Promise.reject('error')
            }
        },
        checkSaveNoLabelThree(){
            let tempTable=[]
            tempTable = this.tableData.filter((item)=>{return item.isSelect !== '1' || item.isSelect === '1' && item.subPrjId === this.prjId})
            console.log('tempTable',tempTable)
            if(+this.bindNum > this.osAmount-tempTable.length){
                msgInfoEl({
                    type:'error',
                    message:'无标签ID物资绑定数量应小于该出库单明细剩余可绑定数量',
                })
                return Promise.reject('error')
            }
        },
        async checkSaveNoLabelFour(){
            const params={
                materialCode:this.categoryCode,
                receiveAmount:this.bindNum,
            }
            await checkMateLabelPrecision(params).then((res)=>{
                if(+res.code !== 200){
                    return Promise.reject('error')
                }
            }).catch(()=>{
                return Promise.reject('error')
            })
        },
        checkSaveNoLabelFive(){
            const flag=this.multipleSelection.some((item)=>!item.subType)
            if(flag){
                msgInfoEl({
                    type:'error',
                    message:'当前勾选签码数据未选择建设子专业，请检查！',
                })
                return Promise.reject('error')
            }
        },
        async saveNoLabel(){
            await this.checkSaveNoLabelOne();
            await this.checkSaveNoLabelTwo();
            //无签码填写了数据
            if(+this.bindNum>0){
                // await this.checkSaveNoLabelThree();   //涉及利旧物资 可选签码长度不一定为1，所以后端进行校验
                await this.checkSaveNoLabelFour();
            }
            //有签码选了数据，判断校验是否通过
            if(this.multipleSelection.length>0){
                await this.checkSaveNoLabelFive();
            }
            const arr=[];
            this.multipleSelection.forEach((item)=>{
                arr.push({
                    ...item,
                    subPrjId:this.prjId,
                })
            })
            const params={
                amount:this.bindNum === ''? 0 : +this.bindNum,
                subPrjId:this.prjId,
                list:arr,
                osDetailId: this.osDetailId,
                outputType: this.outputType,
            }
            await bindAssetLabelList(params).then((res)=>{
                if(+res.code===200){
                    console.log('发送')
                    this.getTable();
                    msgInfoEl({
                        type:'success',
                        message:'保存成功！',
                    })
                }
            })
        },
        saveLabel(){
            if(this.multipleSelection.length === 0){
                return msgInfoEl({
                    type:'error',
                    message:'请选择对应的签码数据',
                })
            }else{
                const flag=this.multipleSelection.some((item)=>!item.subType)
                if(flag){
                    return msgInfoEl({
                        type:'error',
                        message:'当前勾选签码数据未选择建设子专业，请检查！',
                    })
                }else{
                    const arr=[];
                    this.multipleSelection.forEach((item)=>{
                        arr.push({
                            ...item,
                            subPrjId:this.prjId,
                        })
                    })
                    const params={
                        amount:0,
                        subPrjId:this.prjId,
                        list:arr,
                        osDetailId: this.osDetailId,
                        outputType: this.outputType,
                    }
                    bindAssetLabelList(params).then((res)=>{
                        if(+res.code===200){
                            console.log('发送')
                            this.getTable();
                            msgInfoEl({
                                type:'success',
                                message:'保存成功！',
                            })
                        }
                    })
                }
            }
        },
        handleSelectionChange(val) {
            if(this.status){
                this.multipleSelection = val
            }
        },
        setSelection() {
            this.$refs.multipleTable.clearSelection() // 清除所有选项
            this.tableData.forEach((row) => {
                const flag = this.multipleSelection.some(item => item.sourceAssetCode === row.sourceAssetCode)
                if (flag) {
                    this.$refs.multipleTable.toggleRowSelection(row, true)
                }
            })
            this.status=true
        },
        async getTableMultipleSelection() {
            const params={
                ...this.form,
                subPrjId:this.prjId,
                osDetailId: this.osDetailId,
            }
            getAssetLabelList(params).then((res)=>{
                if(+res.code === 200){
                    res.data.list.filter((item)=>{
                        if(+item.isSelect === 0 && item.subSpecList && item.subSpecList.length === 1){
                            item.subType=item.subSpecList[0].subSpecCode
                        }
                    })
                    this.tableData=res.data.list;
                    this.bindNum=+res.data.amount === 0?'':res.data.amount;
                    this.multipleSelection=this.tableData.filter(item=>item.isSelect === '1' && item.subPrjId === this.prjId)
                    console.log(1111,this.multipleSelection,this.tableData)
                    this.setSelection()
                }
            })
        },
        async getTable() {
            const params={
                ...this.form,
                subPrjId:this.prjId,
                osDetailId: this.osDetailId,
            }
            getAssetLabelList(params).then((res)=>{
                if(+res.code === 200){
                    res.data.list.filter((item)=>{
                        if(+item.isSelect === 0 && item.subSpecList && item.subSpecList.length === 1){
                            item.subType=item.subSpecList[0].subSpecCode
                        }
                    })
                    this.tableData=res.data.list;
                    this.bindNum=res.data.amount;
                }
            })
        },
    }

}
</script>

<style scoped lang="scss">

</style>